<template>
  <div>
    <el-dialog
      title="新增校色"
      :visible.sync="isShow"
      width="45%"
      @close="closeHandler"
    >
      <el-form
        ref="form"
        :model="formData"
        :rules="fromRules"
        label-width="130px"
        :inline="false"
        size="small"
      >
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="formData.name" />
        </el-form-item>
        <el-form-item label="角色状态">
          <el-switch
            v-model="formData.state"
            active-text="启用"
            inactive-text="禁用"
            :active-value="1"
            :inactive-value="0"
          />
        </el-form-item>
        <el-form-item label="角色描述" prop="description">
          <el-input v-model="formData.description" />
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button @click="isShow = false">Cancel</el-button>
        <el-button type="primary" @click="onSubmit">OK</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { addSoleApi } from '@/api/role'
export default {
  data() {
    return {
      isShow: false,
      formData: {
        name: '',
        description: '',
        state: 0
      },
      fromRules: {
        name: [{ required: true, message: '请输入', trigger: 'blur' }],
        description: [{ required: true, message: '请输入', trigger: 'blur' }]
      }
    }
  },
  methods: {
    async onSubmit() {
      try {
        // console.log(this.$refs.form)
        await this.$refs.form.validate()
        await addSoleApi(this.formData)
        this.$message.success('添加成功')
        this.isShow = false
        this.$emit('updateList')
      } catch (error) {
        console.log(error)
      }
    },
    closeHandler() {
      this.formData = {
        name: '',
        description: '',
        state: ''
      }
      //   this.$refs.form.clearValidate()
      this.$refs.form.clearValidate()
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
